/***********************************************************************************
**  
* @copyright (c) 2010-2019, ChengDu Duyer Technology Co., LTD. All Right Reserved.
*
************************************************************************************/
/**
* @file	    mjs_client.h
* @version     
* @brief      
* @author   duye
* @date	    2017-01-16
* @note 
*
* 1. 2017-01-16 duye Created this file
*/

#pragma once

#include <duye_system.h>
#include <duye_timer.h>
#include <mjs_inner_msg.h>

class MJSOnActionBase;
class MJSReqer;
class MJServer;
class MJMsgAction;

// <action_name, on_action>
typedef std::map<std::string, MJSOnActionBase*>	MJSOnActionMap;

/**
 * @brief client proxy
 */
class MJSClient
{
public:
	// connect status
	typedef enum {
		ON_LINE,
		OFF_LINE
	} ConnStatus;

	// client type
	typedef enum {
		// mangjingw
		CLIENT_MJW,
		// mangjingc
		CLIENT_MJC,
		// unknown
		CLIENT_UNKNOWN
	} ClientType;

	// authentication status
	typedef enum {
		// not certified
		AUTH_NO,
		// certified failed
		AUTH_FA,
		// certified successed
		AUTH_OK
	} AuthStatus;
	
public:
	MJSClient(const int32 sockfd, const std::string& ip, MJSOnActionMap* on_action_map, MJServer* sender, const ClientType& client_type);
	virtual ~MJSClient();

	void setConnStatus(const ConnStatus status);
	MJSClient::ConnStatus connStatus();
	std::string connStatusStr();

	void setAuthStatus(const AuthStatus status);
	MJSClient::AuthStatus authStatus();
	std::string authStatusStr();

	const std::string& getIP() const;
	const std::string& getUser() const;

	virtual bool handleAction(const std::list<MJMsgAction>& action_list) = 0;

protected:
	bool notify(const MJSInnerMsgType& msg_type, void* data);
	virtual bool innerNotify(const MJSInnerMsgType& msg_type, void* data) = 0;
	bool sendToClient(const int8* data, const uint64 len);

protected:
	friend class MJSReqer;

	ClientType m_client_type;

	int32 m_sockfd;
	std::string m_ip;
	std::string m_user;
	ConnStatus m_conn_status;
	AuthStatus m_auth_status;

	MJSOnActionMap* m_on_action_map;
	MJServer* m_mjserver;
	MJSReqer* m_reqer;
};